package com.dzt.androidframework.changeskin;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.utils.JFileKit;
import com.dzt.androidframework.utils.JLogKit;
import com.dzt.androidframework.widgets.TitleView;
import com.dzt.androidframework.changeskin.lib.base.BaseSkinActivity;
import com.dzt.androidframework.changeskin.lib.utils.L;
import com.nineoldandroids.view.ViewHelper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

public class SkinActivity extends BaseSkinActivity {

	/**
	 * 申请权限的请求码
	 */
	public static final int REQUEST_PERMISSION_CODE = 1;
	private TitleView titleView;
	private DrawerLayout mDrawerLayout;
	private ListView mListView;
	public final static String apkName = "plugin-debug.apk";
	public final static String pkgName = "com.dzt.plugin";
	public static String mSkinPkgPath = Environment.getExternalStorageDirectory() + File.separator + apkName;
	private String[] mDatas = new String[]{"Activity", "Service",
			"Activity", "Service", "Activity", "Service", "Activity", "Service"};
	protected String[] permissions;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_skin);
		permissions = initPermissions();
		if (!checkPermissionIsGranted()) {
			ActivityCompat.requestPermissions(this, permissions, REQUEST_PERMISSION_CODE);
		}
		String path = JFileKit.getDiskCacheDir(this) + File.separator
				+ "plugin" + File.separator;
		try {
			if(!JFileKit.isFileExist(path, apkName)){
				InputStream is = getAssets().open(apkName);
				JFileKit.copyFromInput(path, apkName, is);
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		mSkinPkgPath = path + apkName;
		JLogKit.getInstance().i("path = " + mSkinPkgPath);
		initView();
		initEvents();
	}

	@Override
	public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
		if (requestCode == REQUEST_PERMISSION_CODE) {
			if (!permissionIsGranted(grantResults)) {
				L.e("权限被拒绝，关闭程序");
				finish();
			}
		}
		super.onRequestPermissionsResult(requestCode, permissions, grantResults);
	}

	private boolean permissionIsGranted(int[] grantResults) {
		for (int grant : grantResults) {
			if (grant != PackageManager.PERMISSION_GRANTED) {
				return false;
			}
		}
		return true;
	}

	private boolean checkPermissionIsGranted() {
		if (permissions == null || permissions.length == 0)
			return true;
		for (String permission : permissions) {
			if (ContextCompat.checkSelfPermission(this, permission)
					!= PackageManager.PERMISSION_GRANTED) {
				return false;
			}
		}
		return true;
	}

	protected String[] initPermissions() {
		return new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
				Manifest.permission.READ_EXTERNAL_STORAGE};
	}

	private void initEvents() {
		mListView = (ListView) findViewById(R.id.id_listview);
		mListView.setAdapter(new ArrayAdapter<String>(this, -1, mDatas) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				if (convertView == null) {
					convertView = LayoutInflater.from(SkinActivity.this).
							inflate(R.layout.skin_item, parent, false);
				}

				TextView tv = (TextView) convertView.findViewById(R.id.id_tv_title);
				tv.setText(getItem(position));
				return convertView;
			}
		});


		mDrawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {
			@Override
			public void onDrawerStateChanged(int newState) {
			}

			@Override
			public void onDrawerSlide(View drawerView, float slideOffset) {
				View mContent = mDrawerLayout.getChildAt(0);
				View mMenu = drawerView;
				float scale = 1 - slideOffset;
				float rightScale = 0.8f + scale * 0.2f;

				if (drawerView.getTag().equals("LEFT")) {

					float leftScale = 1 - 0.3f * scale;

					ViewHelper.setScaleX(mMenu, leftScale);
					ViewHelper.setScaleY(mMenu, leftScale);
					ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale));
					ViewHelper.setTranslationX(mContent,
							mMenu.getMeasuredWidth() * (1 - scale));
					ViewHelper.setPivotX(mContent, 0);
					ViewHelper.setPivotY(mContent,
							mContent.getMeasuredHeight() / 2);
					mContent.invalidate();
					ViewHelper.setScaleX(mContent, rightScale);
					ViewHelper.setScaleY(mContent, rightScale);
				}
			}

			@Override
			public void onDrawerOpened(View drawerView) {
			}

			@Override
			public void onDrawerClosed(View drawerView) {
			}
		});
	}

	private void initView() {
		titleView = (TitleView) findViewById(R.id.tv_title);
		titleView.setResource(R.drawable.selector_return_btn,
				R.string.menu_5);
		titleView.setLeftClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);

		FragmentManager fm = getSupportFragmentManager();
		Fragment fragment = fm.findFragmentById(R.id.id_left_menu_container);
		if (fragment == null) {
			fm.beginTransaction().add(R.id.id_left_menu_container,
					new MenuLeftFragment()).commit();
		}
	}
}
